﻿using System;

namespace DiskUsage.CoreLib.MessageBus
{
    /// <summary>
    /// Generic message bus
    /// http://perrybirch.blogspot.com/2007/06/generic-message-bus.html
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MessageBus<T> where T : IMessage
    {
        private static MessageBus<T> m_instance = null;
        private static readonly object m_lock = new object();

        public static MessageBus<T> Instance
        {
            get
            {
                lock (m_lock)
                {
                    if (m_instance == null)
                    {
                        m_instance = new MessageBus<T>();
                    }
                    return m_instance;
                }
            }
        }
        public event EventHandler<MessageBusEventArgs<T>> MessageRecieved;

        public void SendMessage(object sender, T Message)
        {
            if (MessageRecieved != null)
            {
                MessageRecieved(sender, new MessageBusEventArgs<T>(Message));
            }
        }
    }
}
